define(["utils", "base"], function(utils, baseClass) {
    var Component = function(config) {
        var _this = this;
        var placeholder = config.placeholder || "";
        var maxlength = config.maxlength;

        this.input = $("<textarea wrap='hard' placeholder='" + placeholder + "'></textarea>");
        if (maxlength) {
            this.input.attr("maxlength", maxlength);
        }
        Component.baseConstructor.call(this, config);

        this.max = this.config.max;
        if (isNaN(this.max)) {
            this.max = null;
        } else {
            this.max = parseInt(this.max);
        }
        var method_name = config.comKey + "_keydown";
        var method_keydown = _this.pageview.plugin[method_name];
        if (method_keydown) {
            this.$el.bind("keydown", function(e) {
                !this.disabled && method_keydown && method_keydown.call(_this.pageview.plugin, _this, {
                    e: e
                });
            });
        }

        var blurName = config.comKey + '_blur';
        var blurMethod = _this.pageview.plugin[blurName];
        if(blurMethod) {
            this.input.bind('blur', function(e) {
                !this.disabled && blurMethod.call(_this.pageview.plugin, _this, {e: e});
            })
        }

        var keyUpName = config.comKey + "_keyup";
        var keyUpMethod = _this.pageview.plugin[keyUpName];
        if (keyUpMethod) {
            this.$el.bind("keyup", function(e) {
                !this.disabled && keyUpMethod && keyUpMethod.call(_this.pageview.plugin, _this, {
                    e: e
                });
            });
        }
        this.$el.addClass("yy-textarea");
        this.config.style = this.config.style || {};
        var style = {
            fontSize: this.config.style["fontSize"] || this.config.style["font-size"] || "15px",
            color: this.config.style["color"] || this.config.style["color"] || "black",
            backgroundColor: this.config.style["backgroundColor"] || this.config.style["background-color"] || "white",
        };
        var inputStyle = this.config.inputStyle || {};
        this.input.css(style).css(inputStyle)
        this.input.bind("input", function(e) {
            if (_this.max) {
                var val = _this.input.val();
                var length = val.length;
                if (length > _this.max) {
                    val = val.substring(0, _this.max);
                    length = _this.max;
                    _this.input.val(val);
                }
                _this.numDom.html(length);
            }
        });
        if (this.max) {
            var val = _this.input.val();
            var length = val.length? val.length : 0;
            this.limitDom = $("<span class='yy-textarea-limit'></span>");
            this.numDom = $("<span>"+length+"</span>");
            if (this.config.numStyle) {
                utils.css(this.numDom, this.config.numStyle);
            }
            var splitline = $("<span>/</span>");
            var maxDom = $("<span>" + this.max + "</span>");
            if (this.config.maxStyle) {
                utils.css(maxDom, this.config.maxStyle);
            }
            if (this.config.limitStyle) {
                utils.css(this.limitDom, this.config.limitStyle);
            }
            this.limitDom.append(this.numDom).append(splitline).append(maxDom);
            this.$el.append(this.limitDom);
        }
        this.$el.append(this.input);
    }
    utils.extendClass(Component, baseClass);

    Component.prototype.getValue = function() {
        return this.input.val();
    };

    Component.prototype.getText = function() {
        var text = this.input.val()||"";
        text=text.replace(/\r\n/g,"huxiaozhong")
        text=text.replace(/\n/g,"huxiaozhong");
        text = $('<span/>').text(text).text();
        var reg=new RegExp("huxiaozhong","g"); //创建正则RegExp对象
        text=text.replace(reg,"\r\n");
        return text;
    };

    Component.prototype.setDisabled = function(disabled, isNoStyle) {
        this.disabled = disabled;
        if (this.disabled) {
            this.input.attr("disabled", "disabled");
            if (isNoStyle === true) {

            } else {
                this.$el.addClass(this.disabledClassName);
            }
        } else {
            this.input.removeAttr("disabled");
            this.$el.remove(this.disabledClassName);
        }
    };
    Component.prototype.setValue = function(str) {
        if (this.max) {
            var length = str.length;
            if (length > this.max) {
                str = str.substring(this.max);
                length = this.max;
            }
            this.numDom.html(length);
        }
        this.input.val(str);
    };
    Component.prototype.blur = function() {
        this.input.blur();
    };
    Component.prototype.focus = function(str) {
        var _this = this;
        _this.input.focus();
    };
    return Component;
});
